package com.weex.app.module.http;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.Toast;

import com.abe.libhttp.BaseApi;
import com.google.gson.Gson;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import com.taobao.weex.common.WXModule;
import com.weex.app.module.http.api.AppMainService;
import com.weex.app.module.http.entity.UploadInfoModel;
import com.weex.app.util.LocalLogUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UploadFileModule extends WXModule {

    @JSMethod
    public void uploadByUrl(String serviceUrl, String filePath, final JSCallback callback) {
        if (TextUtils.isEmpty(filePath)) return;
        File file = new File(filePath);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData("aFile", file.getName(), requestFile);
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addPart(part)
                .build();
        final Request request = new Request.Builder()
                .url(serviceUrl)
                .post(requestBody)
                .build();
        BaseApi.getOkHttpClient().newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                //数据处理位置
                Map<String, Object> map = new HashMap<>();
                map.put("isUploadSuccess", false);
                map.put("error", e.getMessage());
                callback.invokeAndKeepAlive(map);
            }

            @Override
            public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
                if (response == null || response.body() == null) return;
                final String data = response.body().string();
                ((Activity) mWXSDKInstance.getContext()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(mWXSDKInstance.getContext(), data, Toast.LENGTH_SHORT).show();
                    }
                });
                LocalLogUtils.saveInfo2File(String.valueOf(System.currentTimeMillis()), "***************************" + data);
                UploadInfoModel model = new Gson().fromJson(data, UploadInfoModel.class);
                //数据处理位置
                Map<String, Object> map = new HashMap<>();
                map.put("isUploadSuccess", model.getStatus().equals("1"));
                map.put("error", "上传成功");
                callback.invokeAndKeepAlive(map);
            }
        });
    }

    @JSMethod
    public void upload(String path, final JSCallback callback) {
        if (TextUtils.isEmpty(path)) return;
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("aFile", file.getName(), requestFile);
        Call<UploadInfoModel> call = AppMainService.getCommonService().upload(body);
        call.enqueue(new Callback<UploadInfoModel>() {
            @Override
            public void onResponse(Call<UploadInfoModel> call, Response<UploadInfoModel> response) {
                if (response == null || response.body() == null) return;
                //数据处理位置
                Map<String, Object> map = new HashMap<>();
                map.put("isUploadSuccess", response.body().getStatus().equals("1"));
                map.put("error", "上传成功");
                callback.invokeAndKeepAlive(map);
            }

            @Override
            public void onFailure(Call<UploadInfoModel> call, Throwable t) {
                //数据处理位置
                Map<String, Object> map = new HashMap<>();
                map.put("isUploadSuccess", false);
                map.put("error", t.getMessage());
                callback.invokeAndKeepAlive(map);
            }
        });
    }

    @JSMethod
    public void uploadWithParams(String json, String path, final JSCallback callback) {
        if (TextUtils.isEmpty(path)) return;
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("aFile", file.getName(), requestFile);
        RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), json);
        Call<UploadInfoModel> call = AppMainService.getCommonService().upload(description, body);
        call.enqueue(new Callback<UploadInfoModel>() {
            @Override
            public void onResponse(Call<UploadInfoModel> call, Response<UploadInfoModel> response) {
                if (response == null || response.body() == null) return;
                //数据处理位置
                Map<String, Object> map = new HashMap<>();
                map.put("isUploadSuccess", response.body().getStatus().equals("1"));
                map.put("error", "上传成功");
                callback.invokeAndKeepAlive(map);
            }

            @Override
            public void onFailure(Call<UploadInfoModel> call, Throwable t) {
                //数据处理位置
                Map<String, Object> map = new HashMap<>();
                map.put("isUploadSuccess", false);
                map.put("error", t.getMessage());
                callback.invokeAndKeepAlive(map);
            }
        });
    }
}
